/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Plugins;
import me.zhengjie.modules.system.domain.Pluginsclassify;
import me.zhengjie.modules.system.domain.vo.PluginsclassifyQueryCriteria;
import me.zhengjie.modules.system.service.PluginsService;
import me.zhengjie.modules.system.domain.vo.PluginsQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import me.zhengjie.modules.system.service.PluginsclassifyService;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-04-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "组件管理")
@RequestMapping("/api/plugins")
public class PluginsController {

    private final PluginsService pluginsService;

    private final PluginsclassifyService pluginsclassifyService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('plugins:list')")
    public void exportPlugins(HttpServletResponse response, PluginsQueryCriteria criteria) throws IOException {
        pluginsService.download(pluginsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询组件")
    @ApiOperation("查询组件")
//    @PreAuthorize("@el.check('plugins:list')")
    public ResponseEntity<List<Plugins>> queryPlugins(PluginsQueryCriteria criteria){
        System.out.print(criteria);
        return new ResponseEntity<>(pluginsService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping("/queryPluginsList")
    @Log("查询组件")
    @ApiOperation("查询组件")
//    @PreAuthorize("@el.check('plugins:list')")
    public ResponseEntity<List<Plugins>> queryPluginsListByPluginClassify(@NotNull  @RequestParam("pluginsClassifyId") String pluginsClassifyId){
//        System.out.print("==list1=="+pluginsService.queryPluginsList(pluginsClassifyId));

        return new ResponseEntity<>(pluginsService.queryPluginsList(pluginsClassifyId),HttpStatus.OK);
    }

    @GetMapping("/queryRootpluginclassify")
    @Log("查询分类子集组件")
    @ApiOperation("查询分类子集组件")
    public ResponseEntity <List<Plugins>> queryRootpluginclassify(@NotNull  @RequestParam("pluginsClassifyId") Integer pluginsClassifyId){

        List<Pluginsclassify> pluginsclassifyList3 = pluginsclassifyService.findById(pluginsClassifyId);
        List<Pluginsclassify> pluginsclassifyList1 = pluginsclassifyService.queryplugin(pluginsClassifyId);
        List<Pluginsclassify> pluginsclassifyList2 = pluginsclassifyService.queryRootpluginclassify(pluginsClassifyId);
        List<Integer> list1 = new ArrayList<>();
        for(Pluginsclassify pluginsclassify:pluginsclassifyList1){
            list1.add(pluginsclassify.getId());
        }
        for(Pluginsclassify pluginsclassify:pluginsclassifyList2){
            list1.add(pluginsclassify.getId());
        }
        for(Pluginsclassify pluginsclassify:pluginsclassifyList3){
            list1.add(pluginsclassify.getId());
        }
        HashSet<Integer> set = new HashSet<>(list1);

        List<Integer> list2 = new ArrayList<>();
        list2.addAll(set);

        return new ResponseEntity<>(pluginsService.queryPluginsLevelList(list2),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增组件")
    @ApiOperation("新增组件")
//    @PreAuthorize("@el.check('plugins:add')")
    public ResponseEntity<Object> createPlugins(@Validated @RequestBody Plugins resources){
        pluginsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改组件")
    @ApiOperation("修改组件")
//    @PreAuthorize("@el.check('plugins:edit')")
    public ResponseEntity<Object> updatePlugins(@Validated @RequestBody Plugins resources){
        pluginsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除组件")
    @ApiOperation("删除组件")
//    @PreAuthorize("@el.check('plugins:del')")
    public ResponseEntity<Object> deletePlugins(@RequestBody List<Integer> ids) {
        pluginsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}